#include "PrintWindow.h"
#include "CustomPrintOperation.h"
#include "PrintTable.h"
#include <iostream>


PrintWindow::PrintWindow(AreaDibujo * pd, Circuito* circuito):
btnPageSetup("Configurar Pagina"),
btnPrintPreviewCirc("Previsualizar circuito"),
btnPrintCirc("Imprimir Circuito"),
btnPrintPreviewTable("Previsualizar tabla"),
btnPrintTable("Imprimir Tabla")
{
	//guardo los elementos q recibo por parametro
	this->pDrawing = pd;
	this->circuito = circuito;

	m_refPageSetup = Gtk::PageSetup::create();
	m_refSettings = Gtk::PrintSettings::create();

	set_title("Menu de impresion"); //nombre ventana
	set_default_size(400, 30); //vario tamaño ventana
	set_keep_above();
	build_main_menu();
	//hago q los botones esperen los clicks y llamo a los
	//distintos menus al hacerlo
	get_vbox()->pack_start(hBox, Gtk::PACK_SHRINK);
	show_all_children();
}


void PrintWindow::build_main_menu()
{
	btnPageSetup.signal_clicked().connect(sigc::mem_fun(*this, &PrintWindow::on_menu_file_page_setup));
	btnPrintPreviewCirc.signal_clicked().connect(sigc::mem_fun(*this, &PrintWindow::on_menu_file_print_preview_circ));
	btnPrintCirc.signal_clicked().connect(sigc::mem_fun(*this, &PrintWindow::on_menu_file_print_circ));
	btnPrintPreviewTable.signal_clicked().connect(sigc::mem_fun(*this, &PrintWindow::on_menu_file_print_preview_table));
	btnPrintTable.signal_clicked().connect(sigc::mem_fun(*this, &PrintWindow::on_menu_file_print_table));

	hBox.pack_start(btnPageSetup, Gtk::PACK_EXPAND_WIDGET, 20);
	hBox.pack_start(btnPrintPreviewCirc, Gtk::PACK_EXPAND_WIDGET, 20);
	hBox.pack_start(btnPrintCirc, Gtk::PACK_EXPAND_WIDGET, 20);
	hBox.pack_start(btnPrintPreviewTable, Gtk::PACK_EXPAND_WIDGET, 20);
	hBox.pack_start(btnPrintTable, Gtk::PACK_EXPAND_WIDGET, 20);
}

void PrintWindow::on_printoperation_status_changed_circ(const Glib::RefPtr<CustomPrintOperation>& operation)
{
	if (operation->is_finished())
	{
		Gtk::MessageDialog err_dialog(*this, "Print Finished.", false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
		err_dialog.run();
	}
}

void PrintWindow::on_printoperation_done_circ(Gtk::PrintOperationResult result, const Glib::RefPtr<CustomPrintOperation>& operation)
{
  if (result == Gtk::PRINT_OPERATION_RESULT_ERROR)
  {
    Gtk::MessageDialog err_dialog(*this, "Error printing form", false,
            Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
    err_dialog.run();
  }
  else if (result == Gtk::PRINT_OPERATION_RESULT_APPLY)
  {
    m_refSettings = operation->get_print_settings();
  }

  if (! operation->is_finished())
  {
    operation->signal_status_changed().connect(sigc::bind(sigc::mem_fun(*this,
                    &PrintWindow::on_printoperation_status_changed_circ),
                operation));
  }
}

void PrintWindow::print_or_preview_circ(Gtk::PrintOperationAction print_action)
{
  Glib::RefPtr<CustomPrintOperation> print = CustomPrintOperation::create(this->pDrawing, this->circuito);  // ACA LE PASO EL DRAWING AREA A AL CUSTOMPRINT

  print->set_track_print_status();
  print->set_default_page_setup(m_refPageSetup);
  print->set_print_settings(m_refSettings);

  print->signal_done().connect(sigc::bind(sigc::mem_fun(*this,
                  &PrintWindow::on_printoperation_done_circ), print));
  try
  {
    print->run(print_action /* print or preview */, *this);
  }
  catch (const Gtk::PrintError& ex)
  {
    std::cerr << "An error occurred while trying to run a print operation:"
        << ex.what() << std::endl;
  }

}

void PrintWindow::on_menu_file_page_setup()
{
  Glib::RefPtr<Gtk::PageSetup> new_page_setup = Gtk::run_page_setup_dialog(*this, m_refPageSetup, m_refSettings);
  m_refPageSetup = new_page_setup;
}

void PrintWindow::on_menu_file_print_preview_circ()
{
  print_or_preview_circ(Gtk::PRINT_OPERATION_ACTION_PREVIEW);
}



void PrintWindow::on_menu_file_print_circ()
{
  print_or_preview_circ(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG);
}



void PrintWindow::on_printoperation_status_changed_table(const Glib::RefPtr<PrintTable>& operation)
{
	if (operation->is_finished())
	{
		Gtk::MessageDialog err_dialog(*this, "Print Finished.", false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
		err_dialog.run();
	}
}

void PrintWindow::on_printoperation_done_table(Gtk::PrintOperationResult result, const Glib::RefPtr<PrintTable>& operation)
{
  if (result == Gtk::PRINT_OPERATION_RESULT_ERROR)
  {
    Gtk::MessageDialog err_dialog(*this, "Error printing form", false,
            Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
    err_dialog.run();
  }
  else if (result == Gtk::PRINT_OPERATION_RESULT_APPLY)
  {
    m_refSettings = operation->get_print_settings();
  }

  if (! operation->is_finished())
  {
    operation->signal_status_changed().connect(sigc::bind(sigc::mem_fun(*this,
                    &PrintWindow::on_printoperation_status_changed_table),
                operation));
  }
}

void PrintWindow::print_or_preview_table(Gtk::PrintOperationAction print_action)
{
  Glib::RefPtr<PrintTable> print = PrintTable::create(this->circuito);

  print->set_track_print_status();
  print->set_default_page_setup(m_refPageSetup);
  print->set_print_settings(m_refSettings);

  print->signal_done().connect(sigc::bind(sigc::mem_fun(*this,
                  &PrintWindow::on_printoperation_done_table), print));
  try
  {
    print->run(print_action /* print or preview */, *this);
  }
  catch (const Gtk::PrintError& ex)
  {
    std::cerr << "An error occurred while trying to run a print operation:"
        << ex.what() << std::endl;
  }

}


void PrintWindow::on_menu_file_print_preview_table()
{
  print_or_preview_table(Gtk::PRINT_OPERATION_ACTION_PREVIEW);
}



void PrintWindow::on_menu_file_print_table()
{
  print_or_preview_table(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG);
}




PrintWindow::~PrintWindow(){

}
